package com.huangguolin.cloud.user.control;

import com.huangguolin.cloud.mybatis.entity.PtUserEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PadingRstEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PagingEntity;
import com.huangguolin.cloud.mybatis.ztree.ZtreeEntity;
import com.huangguolin.cloud.spring.control.BaseController;
import com.huangguolin.cloud.user.entity.SearchUserEntity;
import com.huangguolin.cloud.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController extends BaseController {

    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private UserService userService;
    @Secured("ROLE_cldk_data_user_query")
    @RequestMapping("/page")
    public String userPage(){
        return "user/userpage";
    }

    @RequestMapping("/queryUserInfoBypage")
    @ResponseBody
    public PadingRstEntity<PtUserEntity> queryUserInfoBypage(SearchUserEntity entity, PagingEntity paging){

        logger.info(entity);
        logger.info(paging);
        PadingRstEntity<PtUserEntity> entityPadingRstEntity = userService.queryUserInfoBypage(entity,paging);
        return entityPadingRstEntity;
    }

    @RequestMapping("/queryRoleZtreeAll")
    @ResponseBody
    public List<ZtreeEntity> queryRoleZtreeAll(){
        List<ZtreeEntity> list = userService.queryRoleZtreeAll();
        return list;
    }

    @RequestMapping("/saveRoleRefUser")
    @ResponseBody
    public String saveRoleRefUser(String userIds,String roleIds){
        logger.info("userIds:" + userIds);
        logger.info("roleIds:" + roleIds);
        userService.saveRoleRefUser(userIds,roleIds);
        return successJSON("user.allow.role.success");
    }

    @RequestMapping("/queryRoleZtreeByUser")
    @ResponseBody
    public List<ZtreeEntity> queryRoleZtreeByUser (@RequestParam("userId") String userId){
        logger.info("userId:" + userId);
        List<ZtreeEntity> list = userService.queryRoleZtreeByUser(userId);
        return list;
    }

    @RequestMapping("/resetPasswordByUser")
    @ResponseBody
    public String resetPasswordByUser(@RequestParam("userId") String userId,@RequestParam("resetPass") String resetPass){
        userService.resetPasswordByUser(userId,resetPass);
        return successJSON("user.reresetPasswordByUser.success");
    }

    @RequestMapping("/resetPasswordAll")
    @ResponseBody
    public String resetPasswordAll(@RequestParam("userId") String userId,@RequestParam("resetPass") String resetPass){
        String[] userArray = userId.split(",");

        for (String userIds:userArray){
            userService.resetPasswordAll(userIds,resetPass);
        }

        return successJSON("user.resetPasswordAll.success");
    }
}
